home *** CD-ROM | disk | FTP | other *** search
/ Mac Easy 2010 May / Mac Life Ubuntu.iso / casper / filesystem.squashfs / usr / share / doc / python-rdflib / examples / swap_primer.py < prev   
Encoding:
Python Source  |  2007-04-04  |  3.0 KB  |  136 lines

  1. # http://www.w3.org/2000/10/swap/Primer
  2.  
  3. # This is a simple primer using some of the 
  4. # example stuff in the above Primer on N3
  5. # get RDFLib at http://rdflib.net/ 
  6.  
  7.  
  8. # Load up RDFLib
  9.  
  10. from rdflib import *
  11.  
  12. # Firstly, it doesn't have to be so complex.
  13. # Here we create a "Graph" of our work.
  14. # Think of it as a blank piece of graph paper!
  15.  
  16. primer = ConjunctiveGraph()
  17. myNS = Namespace('#')
  18.  
  19. primer.add((myNS.pat, myNS.knows, myNS.jo))
  20. # or:
  21. primer.add((myNS['pat'], myNS['age'], long(24)))
  22.  
  23.  
  24. # Now, with just that, lets see how the system
  25. # recorded *way* too many details about what
  26. # you just asserted as fact.
  27. #
  28.  
  29. from pprint import pprint
  30. pprint(list(primer))
  31.  
  32.  
  33. # just think .whatever((s, p, o))
  34. # here we report on what we know
  35.  
  36. pprint(list(primer.subjects()))
  37. pprint(list(primer.predicates()))
  38. pprint(list(primer.objects()))
  39.  
  40. # and other things that make sense
  41.  
  42. # what do we know about pat?
  43. pprint(list(primer.predicate_objects(myNS.pat)))
  44.  
  45. # who is what age?
  46. pprint(list(primer.subject_objects(myNS.age)))
  47.  
  48.  
  49.  
  50. # Okay, so lets now work with a bigger
  51. # dataset from the example, and start
  52. # with a fresh new graph.
  53.  
  54.  
  55. primer = ConjunctiveGraph()
  56.  
  57.  
  58. # Lets start with a verbatim string straight from the primer text:
  59.  
  60. mySource = """
  61.  
  62.  
  63. @prefix : <http://www.w3.org/2000/10/swap/Primer#>.
  64. @prefix rdf:  <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .
  65. @prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> .
  66. @prefix owl:  <http://www.w3.org/2002/07/owl#> .
  67. @prefix dc:  <http://purl.org/dc/elements/1.1/> .
  68. @prefix foo: <http://www.w3.org/2000/10/swap/Primer#>.
  69. @prefix swap: <http://www.w3.org/2000/10/swap/>.
  70.  
  71. <> dc:title
  72.   "Primer - Getting into the Semantic Web and RDF using N3".
  73.  
  74. <#pat> <#knows> <#jo> .
  75. <#pat> <#age> 24 .
  76. <#al> is <#child> of <#pat> .
  77.  
  78. <#pat> <#child>  <#al>, <#chaz>, <#mo> ;
  79.        <#age>    24 ;
  80.        <#eyecolor> "blue" .
  81.  
  82.  
  83. :Person a rdfs:Class.
  84.  
  85. :Pat a :Person.
  86.  
  87. :Woman a rdfs:Class; rdfs:subClassOf :Person .
  88.  
  89. :sister a rdf:Property.
  90.  
  91. :sister rdfs:domain :Person; 
  92.         rdfs:range :Woman.
  93.  
  94. :Woman = foo:FemaleAdult .
  95. :Title a rdf:Property; = dc:title .
  96.  
  97.  
  98.  
  99. """ # --- End of primer code
  100.  
  101. # To make this go easier to spit back out... 
  102. # technically, we already created a namespace
  103. # with the object init (and it added some namespaces as well)
  104. # By default, your main namespace is the URI of your 
  105. # current working directory, so lets make that simpler:
  106.  
  107. myNS = Namespace(URIRef('http://www.w3.org/2000/10/swap/Primer#'))
  108. primer.bind('', myNS)
  109. primer.bind('owl', 'http://www.w3.org/2002/07/owl#')
  110. primer.bind('dc', 'http://purl.org/dc/elements/1.1/')
  111. primer.bind('swap', 'http://www.w3.org/2000/10/swap/')
  112. sourceCode = StringInputSource(mySource, myNS)
  113.  
  114. # Lets load it up!
  115.  
  116. primer.parse(sourceCode, format='n3')
  117.  
  118.  
  119. # Now you can query, either directly straight into a list:
  120.  
  121. [(x, y, z) for x, y, z in primer]
  122.  
  123. # or spit it back out (mostly) the way we created it:
  124.  
  125. print primer.serialize(format='n3')
  126.  
  127. # for more insight into things already done, lets see the namespaces
  128.  
  129. list(primer.namespaces())
  130.  
  131. # lets ask something about the data
  132.  
  133. list(primer.objects(myNS.pat, myNS.child))
  134.  
  135.  
  136.